package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description:
 * @param:
 * @Return:
 * @Author: 孙维
 * @Create:2021-09-14 22:24
 */

public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set = new HashSet<>();
    /**
     * @Author:孙维
     * @Description://初始化方法
     * @Date: 2021/9/14 22:29
     * @Param:[constraintAnnotation]
     * @return:void
     **/
    @Override
    public void initialize(ListValue constraintAnnotation) {

        int[] vals = constraintAnnotation.vals();
        for(int val : vals){
            set.add(val);
        }
    }
    /**
     * @Author:孙维
     * @Description://判断是否校验成功
     * @Date: 2021/9/14 22:30
     * @Param:[value, context]需要校验的值
     * @return:boolean
     **/
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        return set.contains(value);
    }
}
